package net.guerlab.mall.seller.print.printer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.math.BigDecimal;

import net.guerlab.mall.seller.print.entity.order.GoodsInfo;
import net.guerlab.mall.seller.print.entity.order.OrderInfo;
import static net.guerlab.mall.seller.print.printer.PrintHelper.getPoint;

public class OrderPrintableImpl implements Printable {

    private static final String FONT_NAME = "新宋体";

    private static final int FONT_TYPE = Font.PLAIN;

    private OrderInfo orderInfo;

    private boolean isFirst = true;

    public OrderPrintableImpl(OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
    }

    /**
     * @param Graphic
     *            指明打印的图形环境
     * @param PageFormat
     *            指明打印页格式 （页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸大致为595×842点）
     * @param pageIndex指明页号
     **/
    public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
        if (pageIndex != 0) {
            return NO_SUCH_PAGE;
        }
        if (isFirst) {
            isFirst = false;
            return PAGE_EXISTS;
        }

        Graphics2D g = (Graphics2D) gra;

        g.setColor(Color.black);

        float[] dash1 = { 2.0f };
        // 设置打印线的属性。
        // 1.线宽 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
        g.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));

        float x = (float) pf.getImageableX();
        float y = (float) pf.getImageableY();

        final float leftOffset = x + getPoint(5);
        
        float heightOffset = y + getPoint(5);

        g.setFont(getFont(6));
        g.drawString("孝昕菜园", x + getPoint(16), heightOffset);

        heightOffset += getPoint(6);
        g.setFont(getFont(5));
        g.drawString("送货单", x + getPoint(22), heightOffset);

        g.setFont(getFont(3));
        heightOffset += getPoint(7);
        g.drawString("订单编号:", leftOffset, heightOffset);
        
        heightOffset += getPoint(5);
        g.drawString(orderInfo.getOrderSn(), leftOffset, heightOffset);
        
        final float nameOffset = leftOffset;
        
        heightOffset += getPoint(5);

        g.drawString("名称", nameOffset, heightOffset);
        
        final float quantityOffset = nameOffset;
        final float priceOffset = x + getPoint(18);
        final float totleOffset = x + getPoint(35);
        
        heightOffset += getPoint(5);
        
        g.drawString("数量", quantityOffset, heightOffset);
        g.drawString("单价", priceOffset, heightOffset);
        g.drawString("金额", totleOffset, heightOffset);
        
        heightOffset += getPoint(5);
        
        g.drawString("(Kg)", quantityOffset, heightOffset);
        g.drawString("(元/Kg)", priceOffset, heightOffset);
        g.drawString("(元)", totleOffset, heightOffset);

        float nowHeightOffset = heightOffset + getPoint(5);
        for (GoodsInfo goodsInfo : orderInfo.getGoodsInfos()) {

            if (goodsInfo.getGoodsName().length() > 12) {
                g.drawString(goodsInfo.getGoodsName().substring(0, 13), nameOffset, nowHeightOffset);
            } else {
                g.drawString(goodsInfo.getGoodsName(), nameOffset, nowHeightOffset);
            }
            
            nowHeightOffset += getPoint(4);
            
            g.drawString(goodsInfo.getQuantity(), quantityOffset, nowHeightOffset);
            
            BigDecimal price = new BigDecimal(goodsInfo.getPrice());
            
            g.drawString(goodsInfo.getQuantity(), quantityOffset, nowHeightOffset);
            g.drawString(price.setScale(2, BigDecimal.ROUND_DOWN).toString(), priceOffset, nowHeightOffset);
            
            g.drawString("￥" +goodsInfo.getTotlePrice(), totleOffset, nowHeightOffset);
            
            nowHeightOffset += getPoint(4);
        }

        g.setFont(getFont(4));

        nowHeightOffset += getPoint(10);

        g.drawString("合计金额:￥" + orderInfo.getOrderAmount(), leftOffset, nowHeightOffset);

        String addressString = "配送地址:" + orderInfo.getRegionName() + orderInfo.getDetailedAddress();

        addressString = addressString.replace("\t", "");

        final int lineTextNum = 11;

        final int lineNum = (int) Math.ceil(addressString.length() * 1.0 / lineTextNum);

        nowHeightOffset += getPoint(4);

        for (int i = 0; i < lineNum; i++) {
            nowHeightOffset += getPoint(4);

            int end = addressString.length();
            if (i != (lineNum - 1)) {
                end = (i + 1) * lineTextNum;
            }

            g.drawString(addressString.substring(i * lineTextNum, end), leftOffset, nowHeightOffset);
        }

        nowHeightOffset += getPoint(6);

        g.drawString("联系方式:" + orderInfo.getMobilePhone(), leftOffset, nowHeightOffset);

        nowHeightOffset += getPoint(6);

        g.drawString("配送员:" + orderInfo.getPersonnelName(), leftOffset, nowHeightOffset);

        // g.setColor(Color.WHITE);

        nowHeightOffset += getPoint(15);

        g.drawString("------------", leftOffset, nowHeightOffset);

        return PAGE_EXISTS;
    }

    private static Font getFont(int fontSize) {
        return new Font(FONT_NAME, FONT_TYPE, getPoint(fontSize));
    }

}
